package com.zhzh.sc.demo.juc.interview;

/**
 * 连个线程交替打印 A1B2C3D4E5F6G7
 *
 * @author: dasouche
 * @date: 2021-03-17 10:39
 **/
public class A1B2C3Interview {

    public static void main(String[] args) {
        final Object o = new Object();

        char[] aI = "1234567".toCharArray();
        char[] aC = "ABCDEFG".toCharArray();

        new Thread(() -> {
            synchronized (o) {
                for (char c : aI) {
                    System.out.print(c);
                    o.notify();
                    try {
                        o.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                o.notify();
                System.out.println();
            }
        }, "t1").start();

        new Thread(() -> {
            synchronized (o) {
                for (char c : aC) {
                    System.out.print(c);
                    o.notify();
                    try {
                        o.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                o.notify();
                System.out.println();
            }
        }, "t2").start();

    }
}
